<?php


namespace paper\view\driver;


class Interpreter
{

    //实验性部分-----------------
    private array $blocks = [];
    private bool $inChildren = false;
    private string $parent = '';

    /**
     * 标签定义：
     * attrs 属性列表
     * close 是否需要闭合标签（true 或者false 默认false）
     * expression 是否支持表达式（表达式和数学不可同时存在）
     * attr 支持的属性项目
     */

    public $tags = [
        'extend'   => ['need_closed' => false, 'expression' => true],
        'raw'      => ['need_closed' => false, 'expression' => false],
        'block'    => ['need_closed' => true, 'expression' => true],
        'variable' => ['need_closed' => false, 'expression' => true],
        'comment'  => ['need_closed' => false, 'expression' => true],
        'function' => ['need_closed' => false, 'expression' => true],
        'assign'   => ['need_closed' => false, 'expression' => false],
        'php'      => ['need_closed' => true, 'expression' => true],
        'if'       => ['need_closed' => true, 'expression' => true],
        'else'     => ['need_closed' => false, 'expression' => true],
        'elseif'   => ['need_closed' => false, 'expression' => true],
        'foreach'  => ['need_closed' => true, 'expression' => true],
        'for'      => ['need_closed' => true, 'expression' => true],
    ];


    public function parseAssignTag($attrs)
    {
        return '<?php $' . $attrs['name'] . ' = $' . $attrs['value'] . '?>';
    }

    /**
     * @param $expression
     * @param $content
     * @return string
     * @noinspection PhpUnused
     */
    public function parsePhpTag($expression, $content)
    {
        return '<?php ' . $content . ' ?>';
    }

    /**
     * @param $expression
     * @param $content
     * @return string
     * @noinspection PhpUnused
     */
    public function parseIfTag($expression, $content)
    {
        $out = '<?php if(' . $expression . '): ?>';
        $out .= $content;
        $out .= '<?php endif; ?>';
        return $out;
    }

    public function parseElseTag($attrs)
    {
        return '<?php else: ?>';
    }

    public function parseElseIfTag($expression)
    {
        return '<?php elseif(' . $expression . '): ?>';
    }

    /**
     * @param $expression
     * @param $content
     * @return string
     * @noinspection PhpUnused
     */
    public function parseLiteralTag($expression, $content)
    {
        return $content;
    }


    /**
     * @param $specialTag
     * @return string
     * @noinspection PhpUnused
     */
    public function parseRawTag($specialTag)
    {
        return '<?=' . $specialTag . ';?>';
    }


    /**
     * 解析 foreach
     * @param $expression
     * @param $content
     * @return string
     * @noinspection PhpUnused
     */
    public function parseForeachTag($expression, $content)
    {
        $out = '<?php foreach(' . $expression . '): ?>';
        $out .= $content;
        $out .= '<?php endforeach; ?>';
        return $out;
    }

    /**
     * 处理{* xxxx *}类标签
     * @param $expression
     * @return string
     */
    public function parseCommentTag($expression)
    {
        return '<?php /* ' . $expression . ' */ ?>';
    }

    /**
     * 处理{:xxx}类标签
     * @param $expression
     * @return string
     */
    public function parseFunctionTag($expression)
    {
        return '<?=' . $expression . ';?>';
    }

    /**
     * @param $expression
     * @return string
     */
    public function parseVariableTag($expression)
    {
        $filters = explode('|', $expression);
        $varName = '$' . array_shift($filters);
        $raw     = false;
        if ($filters) {
            if ($filters[0] === 'raw') {
                $raw = true;
                array_shift($filters);
            }
            foreach ($filters as $filter) {
                switch ($filter) {
                    case "date":
                        $varName = 'date("Y/m/d H:i:s",' . $varName . ')';
                        break;
                    default:
                        $varName = $filter . '(' . $varName . ')';
                        break;
                }
            }
        }
        if ($raw) {
            return '<?=' . $varName . ';?>';
        }
        return '<?=$this->filter(' . $varName . ');?>';
    }



    /**
     * @param bool $model
     * @return void
     */

    /**
     * @return string
     */
    public function getParent(): string
    {
        return $this->parent;
    }

    /**
     * @return bool
     */
    public function useExtend(): bool
    {
        return $this->inChildren;
    }

    public function setInChildren($model = false)
    {
        $this->inChildren = $model;
    }

    public function parseExtendTag($expression)
    {
        $this->inChildren = true;
        $this->parent     = trim($expression, "\"\'");
    }

    public function parseBlockTag($expression, $body)
    {
        if ($this->inChildren) {
            $this->blocks[$expression] = $body;
            return '';
        }
        return $this->blocks[$expression] ?? $body;
    }
}